import bge
import PKinput
#logic = bge.logic
#cont = logic.getCurrentController()
#own = cont.owner

#CHARACTER MOTION#
######################################

def charRot(fltSpeed, intAxis):
    own = bge.logic.getCurrentController().owner
    """ 012 = xyz, only does one axis, but stackable 
        own = owner
    """
    rotmat = [0.0,0.0,0.0]
    rotmat[intAxis] = fltSpeed
    own.applyRotation(rotmat,True)

def CharMove(fltSpeed, intAxis):
    own = bge.logic.getCurrentController().owner
    """ 012 = xyz, only does one axis, but takes into consideration current velocity 
    works best with character physics
    own = owner
    """
    movement = own.getVelocity()
    movement[intAxis] = fltSpeed
    own.applyMovement(movement, True)
    
def VeloMove(fltSpeed, intAxis):
    own = bge.logic.getCurrentController().owner
    """ 012 = xyz, only does one axis, but takes into consideration current velocity 
        own = owner
    """
    #print(dir(own))
    movement = own.getVelocity()
    movement[intAxis] = fltSpeed
    #own.angularVelocity = movement
    own.setLinearVelocity(movement, True)
    #print(dir(own))
    #own.applyMovement(movement, True)

#END CHARACTER MOTION#
######################################
#CHAR MOVEMENT#
#############################################################

def axisStrafe(strPosKey, strNegKey, strKeySen, floatSpeed, intAxis):
    own = bge.logic.getCurrentController().owner
    """
    012 = xyz,
    setup character strafing, works best with character physics
    """
    currSpeed = 0.0
    #CharRot(fltSpeed, intAxis)
    if(PKinput.keyInput(strKeySen, strPosKey, 2)):
        currSpeed = floatSpeed
        CharMove(currSpeed, intAxis)
    elif(PKinput.keyInput(strKeySen, strNegKey, 2)):
        currSpeed = floatSpeed*-1
        CharMove(currSpeed, intAxis)
        
def axisRotate(strPosKey, strNegKey, strKeySen, floatSpeed, intAxis):
    own = bge.logic.getCurrentController().owner
    """ 012 = xyz """
    currSpeed = 0.0
    #CharRot(fltSpeed, intAxis)
    if(PKinput.keyInput(strKeySen, strPosKey, 2)):
        currSpeed = floatSpeed
        charRot(currSpeed, intAxis)
    elif(PKinput.keyInput(strKeySen, strNegKey, 2)):
        currSpeed = floatSpeed*-1
        charRot(currSpeed, intAxis)
        
def strafeSetup():
    """
    single-line strafing setup for python module brick
    BaySik.strafeSetup
    * must have keyboard sensor named "Keys" set to all keys and positive
    * must have float property for "speed"
    """
    own = bge.logic.getCurrentController().owner
    #print(own.sensors)
    if("Keys" in own.sensors):
        #print(dir(own.sensors["Keys"]))
        if("speed" not in own.getPropertyNames()):
            own["speed"] = .1

        axisStrafe("w", "s", "Keys", own["speed"], 1)
        axisStrafe("d", "a", "Keys", own["speed"], 0)
        
def movementSetup():
    """
    single-line movement setup for python module brick
    BaySik.movementSetup
    * must have keyboard sensor named "Keys" set to all keys and positive
    * must have float property for "speed"
    """
    own = bge.logic.getCurrentController().owner
    #print(own.sensors)
    if("Keys" in own.sensors):
        #print(dir(own.sensors["Keys"]))
        if("speed" not in own.getPropertyNames()):
            own["speed"] = .1

        axisStrafe("w", "s", "Keys", own["speed"], 1)
        axisRotate("d", "a", "Keys", own["speed"], 0)
    

#END CHAR MOVEMENT#
#############################################################

#ROTATION MOUSELOOK#
##############################################################

# I'm aware of the actuator, I'm going to do some epic setups 

def MouseRot3d(booShow, intMouseV, intRotV):
    """
    intMouseV : 0 = horizontal : 1 = vertical
    intRotV : 012 = xyz
    """
    logic = bge.logic
    own = logic.getCurrentController().owner
    
    # put dis pointer here (center to screen)
    logic.mouse.position = (.5, .5) # this only registers after the first frame, causing the object to look up-ish
    # pointer visibility (you may want a reticle)
    logic.mouse.visible = booShow
    # divide the max rotations
    rotdiv = 2.0 / 360
    # get mouse position, change to euler, seperate mouse positions
    mousePos = logic.mouse.position 
    euler = own.localOrientation.to_euler()
    #mouse0 = (mousePos[0]-.5)*-1
    #mouse1 = (mousePos[1]-.5)*-1
    euler[intRotV] += round((mousePos[intMouseV]-.5)*-1, 2)
    mousespeed = (mousePos[intMouseV]-.5)*-1
    #print(mousePos[intMouseV]-.5)
    #charRot(mousespeed, intRotV)
    

    # assign euler to matrix
    own.localOrientation = euler.to_matrix()
    

def MouseLook(floatHlim, floatVlim, booShow, intState):
    """
    1 = vertical : 2 = horizontal : other = both
    was originally 3 seperate modules, you're welcome.
    """
    logic = bge.logic
    own = logic.getCurrentController().owner
    
    # put dis pointer here (center to screen)
    logic.mouse.position = (.5, .5) # this only registers after the first frame, causing the object to look up-ish
    # pointer visibility (you may want a reticle)
    logic.mouse.visible = booShow
    # divide the max rotations
    rotdiv = 2.0 / 360
    # get mouse position, change to euler, seperate mouse positions
    mousePos = logic.mouse.position 
    euler = own.localOrientation.to_euler()
    mouse0 = round((mousePos[0]-.5)*-1, 2)
    mouse1 = round((mousePos[1]-.5)*-1, 2)
    
    # controls mouse-powered rotation
    def toEuler0():
        if(euler[0] <= rotdiv * floatVlim and mouse1 > 0):
            euler[0] += mouse1
            
        if(euler[0] >= rotdiv * floatVlim * -1 and mouse1 < 0):
            euler[0] += mouse1
     
    def toEuler2():   
        if(euler[2] <= rotdiv * floatHlim and mouse0 > 0):
            euler[2] += mouse0
            
        if(euler[2] >= rotdiv * floatHlim * -1 and mouse0 < 0):
            euler[2] += mouse0
            
    if(intState == 1):
        toEuler0()
    elif(intState == 2):
        toEuler2()
    else:
        toEuler0()
        toEuler2()

    # assign euler to matrix
    own.localOrientation = euler.to_matrix()

def camMouse():
    MouseLook(200.0, 200.0, True, 1)
            
def PlayerRotMouse():
    MouseRot3d( True, 0, 2)
    
def lineMove():
    logic = bge.logic
    own = logic.getCurrentController().owner
    if("speed" not in own.getPropertyNames()):
        own["speed"] = .1
    CharMove(own["Speed"], 1)

#END ROTATION MOUSELOOK#
##############################################################
